﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class MovieEndPoints
    {
        private const string MoviesUrl = "api/movies";
        private HttpClient client;

        public MovieEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<List<MovieViewModel>> GetAllMoviesAsync()
        {
            List<MovieViewModel> movies = new List<MovieViewModel>();
            HttpResponseMessage response = await client.GetAsync(MoviesUrl);
            if (response.IsSuccessStatusCode)
            {
                movies = await response.Content.ReadAsAsync<List<MovieViewModel>>();
            }
            return movies;
        }

        public async Task<MovieViewModel> GetMovieAsync(int id)
        {
            MovieViewModel movie = null;
            HttpResponseMessage response = await client.GetAsync(MoviesUrl + "?id=" + id);
            if (response.IsSuccessStatusCode)
            {
                movie = await response.Content.ReadAsAsync<MovieViewModel>();
            }
            return movie;
        }

        public async Task CreateMovieAsync(MovieViewModel movie)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(MoviesUrl, movie);
            response.EnsureSuccessStatusCode();
        }

        public async Task UpdateMovieAsync(MovieViewModel movie)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(MoviesUrl, movie);
            response.EnsureSuccessStatusCode();
        }

        public async Task DeleteMovieAsync(int id)
        {
            HttpResponseMessage response = await client.DeleteAsync(MoviesUrl + "?id=" + id);
            response.EnsureSuccessStatusCode();
        }
    }
}
